<?php
    if (!isset ($_GET["id"]) || !is_numeric($_GET["id"])){
        throw new Exception("Codigo da carga invalido");
    }else{
        include_once $app."dao/dao_carga.php";
        include_once $app."dao/dao_cliente.php";
        $dao = new dao_carga();
        $to = $dao->select($_GET["id"]);
        if ($to == null){
            echo 'Erro, carga n&atilde;o localizada';
            return;
        }
        include_once $app."dao/dao_cliente.php";
        $dao_cliente = new dao_cliente();
        $to_cliente = $dao_cliente->select($to->CLI_ID);
    }
?>
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false&language=pt-BR"></script>
<script>
    
    $(function(){
        initialize();
        calcRoute();
    });
    
    // Google maps
    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;
    var brasil = new google.maps.LatLng(-14.790141, -51.37207);

    
    function initialize() {
      directionsDisplay = new google.maps.DirectionsRenderer();
      var mapOptions = {
//        zoom: 4,
        mapTypeId: google.maps.MapTypeId.ROADMAP
//        center: brasil
      }
      map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
      directionsDisplay.setMap(map);
    }

    function calcRoute() {
      var request = {
          origin: new google.maps.LatLng(<?php echo $to->getCID_ORIGEM_LAT() ?>, <?php echo $to->getCID_ORIGEM_LNG() ?>),
          destination: new google.maps.LatLng(<?php echo $to->getCID_DESTINO_LAT() ?>, <?php echo $to->getCID_DESTINO_LNG() ?>),
          // Note that Javascript allows us to access the constant
          // using square brackets and a string value as its
          // "property."
          travelMode: google.maps.TravelMode.DRIVING
      };
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            $("#map_mensagem").hide();
        }else if(status == google.maps.DirectionsStatus.ZERO_RESULTS){
            var mapOptions = {
                zoom: 4,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: brasil
            }
            map.setOptions(mapOptions);
            
            var markerOrigem = new google.maps.Marker({
                position: new google.maps.LatLng(<?php echo $to->getCID_ORIGEM_LAT() ?>, <?php echo $to->getCID_ORIGEM_LNG() ?>),
                title: "<?php echo $to->getCID_ORIGEM_NOME() . '/' .$to->getEST_ORIGEM_SIGLA() ?>"
            });
            
            var markerDestino = new google.maps.Marker({
                position: new google.maps.LatLng(<?php echo $to->getCID_DESTINO_LAT() ?>, <?php echo $to->getCID_DESTINO_LNG() ?>),
                title: "<?php echo $to->getCID_DESTINO_NOME() . '/' .$to->getEST_DESTINO_SIGLA() ?>"
            });

            // To add the marker to the map, call setMap();
            markerOrigem.setMap(map);
            markerDestino.setMap(map);
            $("#map_mensagem").html("Desculpe-nos, o google n&atilde;o pode encontrar uma rota entre esses destinos!").show();
        }
      });
    }
</script>
<span class="titulo_conteudo">Detalhes da Carga</span>
<div class="corpo_conteudo">
    <table cellspacing="0" width="100%">
        <tr class="tr_carga"><td class="td_tipo" width="90">Origem:</td><td width="180" style="font-weight: bold"><?php echo $to->getCID_ORIGEM_NOME() . '/' .$to->getEST_ORIGEM_SIGLA() ?></td><td class="td_tipo" width="80">Ve&iacute;culo:</td><td width="140"><?php echo $to->getCAR_VEICULO() ?></td><td class="td_tipo" width="45">Data:</td><td><?php echo $to->getCAR_DATA_REGISTRO() ?></td><td rowspan="5" class="td_logo_cliente" width="100"><img src="imagens/clientes/cliente_100x75.php?id=<?php echo $to->CLI_ID ?>" width="100" height="75" style="vertical-align: middle"/></td></tr>
        <tr class="tr_carga"><td class="td_tipo" width="90">Destino:</td><td width="180" style="font-weight: bold"><?php echo $to->getCID_DESTINO_NOME() . '/' .$to->getEST_DESTINO_SIGLA() ?></td><td class="td_tipo" width="80">Carroceria:</td><td width="140"><?php echo $to->getCAR_CARROCERIA() ?></td><td class="td_tipo" width="45">Peso:</td><td><?php echo $to->getCAR_PESO() ?></td></tr>
        <tr class="tr_carga"><td class="td_tipo" width="90">Produto:</td><td width="180"><?php echo $to->getCAR_PRODUTO() ?></td><td class="td_tipo" width="80">Rastreado:</td><td width="140"><?php echo $to->getCAR_RASTREADO()? "Sim" : "N&atilde;o" ?></td><td class="td_tipo" width="45">Valor:</td><td><?php echo $to->getCAR_VALOR() ?></td></tr>
        <tr class="tr_carga"><td class="td_tipo" width="90">Especie:</td><td width="100"><?php echo $to->getCAR_ESPECIE() ?></td><td class="td_tipo" width="80">Validade:</td><td colspan="3" style="font-weight: bold" width="140"><?php echo $to->getCAR_DATA_VALIDADE() ?></td></tr>
        <tr><td class="td_tipo" width="90">Observa&ccedil;&atilde;o:</td><td colspan="5"><?php echo $to->getCAR_OBSERVACAO() ?></td></tr>
    </table>
</div>
<div class="linha_conteudo"></div>
<div class="corpo_conteudo">
    <table cellspacing="0" width="100%">
        <tr class="tr_carga"><td class="td_tipo">Embarcador:</td><td colspan="3" style="font-weight: bold"><?php echo $to_cliente->getCLI_FANTASIA() ?></td><td class="td_tipo">MSN:</td><td colspan="3"><?php echo $to_cliente->getCLI_MSN() ?></td></tr>
        <tr class="tr_carga"><td class="td_tipo">E-mail:</td><td colspan="3"><?php echo $to_cliente->getCLI_EMAIL() ?></td><td class="td_tipo">SKYPE:</td><td colspan="3"><?php echo $to_cliente->getCLI_SKYPE() ?></td></tr>
        <tr class="tr_carga"><td class="td_tipo" width="90">Telefone 1:</td><td width="100"><?php echo $to_cliente->getCLI_TEL1() ?></td><td class="td_tipo" width="80">Telefone 2:</td><td width="100"><?php echo $to_cliente->getCLI_TEL2() ?></td><td class="td_tipo" width="80">Celular:</td><td width="100"><?php echo $to_cliente->getCLI_CELULAR() ?></td><td class="td_tipo" width="80">Fax:</td><td width="100"><?php echo $to_cliente->getCLI_FAX() ?></td></td></tr>
    </table>
</div>
<div class="linha_conteudo"></div>
<div class="corpo_conteudo">
    <div id="map_canvas" style="width: auto; height: 400px"></div>
    <div id="map_mensagem" style="width: auto; text-align: center; color: #060; font-weight: bold; font-size: 15px; padding: 4px 0"></div>
</div>
<div class="linha_conteudo"></div>
<div class="corpo_conteudo" style="text-align: right">
    <form>
        <input type="button" value="Inicio" onclick="window.location = 'index.php'"/>
    </form>  
    <br/><br/>
    <div class="fb-comments" data-href="<?php 
        $protocolo = (strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === false) ? 'http' : 'https';
        $host = $_SERVER['HTTP_HOST'];
        $script = $_SERVER['SCRIPT_NAME'];
        $parametros = $_SERVER['QUERY_STRING'];
        $UrlAtual = $protocolo . '://' . $host . $script . '?' . $parametros;
 
        echo $UrlAtual;
    ?>" data-width="760" data-num-posts="10"></div>
</div>
